package org.csu.mypetstoreplus.persistence;

import jakarta.transaction.Transactional;
import org.csu.mypetstoreplus.entity.CartItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CartItemDao extends JpaRepository<CartItem, Integer> {
    @Query(value = "select * from cartitem where userid =?1", nativeQuery = true)
    List<CartItem> findByUsername(String username);

    @Query(value = "select * from cartitem where userid =?1 and itemid = ?2 limit 1", nativeQuery = true)
    CartItem findByUsernameAndItemId(String username, String itemId);

    @Transactional
    @Modifying
    @Query(value = "delete from cartitem where userid =?1 and itemid = ?2", nativeQuery = true)
    void actuallyDeleteByUsername(String username, String itemId);

    @Transactional
    @Modifying
    @Query(value = "update cartitem set quantity = 0 where userid =?1", nativeQuery = true)
    void deleteByUsername(String username);
}
